package com.scott.itcast.queue;

import java.util.Collections;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class BlockingQueueCommunication {

	public static void main(String[] args) {
		
		final Business business = new Business();
		new Thread(
				new Runnable() {
					public void run() {
					
						for(int i=1;i<=50;i++){
							business.sub(i);
						}
						
					}
				}
		).start();
		
		for(int i=1;i<=50;i++){
			business.main(i);
		}
		
	}

	 static class Business {
		 
		 
		  BlockingQueue<Integer> queue1 = new ArrayBlockingQueue<Integer>(1);
		  BlockingQueue<Integer> queue2 = new ArrayBlockingQueue<Integer>(1);
		  
		  //匿名构造方法 运行时机：在任何构造方法之前
		  {
			  try {
				  //Collections.synchronizedMap(null);
				  System.out.println("xxxxxdfsdsafdsa");
				queue2.put(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		  }
		  
		  public  void sub(int i){
			  	try {
					queue1.put(1);
					for(int j=1;j<=10;j++){
						System.out.println("子线程["+Thread.currentThread().getName()+ "]。外层循环次数：[" + i + "]。内存循环次数：["+j+"]");
					}
					queue2.take();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
		  }
		  
		  public  void main(int i){
			  	try {
					queue2.put(1);
					for(int j=1;j<=100;j++){
						System.out.println("主线程["+Thread.currentThread().getName()+ "]。外层循环次数：[" + i + "]。内存循环次数：["+j+"]");
					}
					queue1.take();
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
		  }
	  }

}
